package com.qn.common.core.result;

import com.qn.common.core.constant.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 统一返回结果类
 *
 * @author Qning
 * @date 2021/05/30
 */
@Data
@ApiModel(value = "统一返回结果类")
public class Result<T> {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public static <T> Result<T> ok() {
        return restResult(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getCode(), null, ResultCodeEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> ok(T data) {
        return restResult(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getCode(), data, ResultCodeEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> ok(String message) {
        return restResult(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getCode(), null, message);
    }

    public static <T> Result<T> ok(T data, String message) {
        return restResult(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getCode(), null, ResultCodeEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> error() {
        return restResult(ResultCodeEnum.UNKNOWN_ERROR.getSuccess(), ResultCodeEnum.UNKNOWN_ERROR.getCode(), null, ResultCodeEnum.UNKNOWN_ERROR.getMessage());
    }

    public static <T> Result<T> error(String message) {
        return restResult(ResultCodeEnum.UNKNOWN_ERROR.getSuccess(), ResultCodeEnum.UNKNOWN_ERROR.getCode(), null, message);
    }

    public static <T> Result<T> error(T data) {
        return restResult(ResultCodeEnum.UNKNOWN_ERROR.getSuccess(), ResultCodeEnum.UNKNOWN_ERROR.getCode(), data, ResultCodeEnum.UNKNOWN_ERROR.getMessage());
    }

    public static <T> Result<T> error(T data, String message) {
        return restResult(ResultCodeEnum.UNKNOWN_ERROR.getSuccess(), ResultCodeEnum.UNKNOWN_ERROR.getCode(), data, message);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return restResult(ResultCodeEnum.UNKNOWN_ERROR.getSuccess(), code, null, message);
    }

    public static <T> Result<T> setResult(ResultCodeEnum resultCodeEnum) {
        return restResult(resultCodeEnum.getSuccess(), resultCodeEnum.getCode(), null, resultCodeEnum.getMessage());
    }

    public static <T> Result<T> setResult(ResultCodeEnum resultCodeEnum, String message) {
        return restResult(resultCodeEnum.getSuccess(), resultCodeEnum.getCode(), null, message);
    }

    private static <T> Result<T> restResult(Boolean success, Integer code, T data, String msg) {
        Result<T> result = new Result<>();
        result.setSuccess(success);
        result.setCode(code);
        result.setData(data);
        result.setMessage(msg);
        return result;
    }

}
